﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration.Provider;
using System.Linq;
using System.Text;
using System.Threading;

namespace Medianamik.Core
{
    internal class ConfigProvider : ProviderBase
    {
        #region Fields
        
        private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();
        
        #endregion

        #region .ctors

        public ConfigProvider()
        {
            Initialize("ConfigProvider", new NameValueCollection());
        }

        #endregion

        #region Properties

        public static ConfigProvider Instance
        {
            get { return Singleton<ConfigProvider>.Instance; }
        }

        #endregion

        #region Methods

        public override void Initialize(string name, NameValueCollection config)
        {
            base.Initialize(name, config);

            Load();
        }

        public void Reload()
        {
            _lock.EnterReadLock();
            try
            {
                Load();
            }
            finally
            {
                _lock.ExitReadLock();
            }
        }

        private void Load()
        {
            _allConfigurations = DALProviderManager.DALProvider.ConfigDALProvider.GetAllConfigurations();
        }

        private IDictionary<String, String> _allConfigurations;
        public IDictionary<String, String> AllConfigurations
        {
            get
            {
                _lock.EnterReadLock();
                try
                {
                    return _allConfigurations;
                }
                finally
                {
                    _lock.ExitReadLock();
                }
            }
        }

        #endregion
    }
}
